﻿#region copyright
// Halcyone - Silverlight Application Framework
// Copyright (C) 2009 - 2010 Alexey Zakharov (B&Z LLC)
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#endregion
namespace ServiceModel.Tests
{
    using System;
    using System.Collections.Generic;

    using Microsoft.Silverlight.Testing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Witcraft.Halcyone.ServiceModel;

    [TestClass]
    public class RestExtensionsTests : SilverlightTest
    {
        [TestMethod]
        [Asynchronous]
        public void OperatoionWithComplexInputParameter()
        {
            var restService = new FooServiceClient();
            IDisposable result = restService.OperatoionWithComplexInputParameter(new FooParameter()
                {
                    Name = "Apple",
                    Price = 300
                })
                .Subscribe
                (
                    r =>
                    {
                        Assert.AreEqual(r, 123);
                        this.EnqueueTestComplete();
                    }
                );
        }

        [TestMethod]
        [Asynchronous]
        public void OperatoionWithComplexInputParameterSendedViaRequestStream()
        {
            var restService = new FooServiceClient();
            IDisposable result = restService.OperatoionWithComplexInputParameterSendedViaRequestStream(new FooParameter()
            {
                Name = "Apple",
                Price = 300
            })
                .Subscribe
                (
                    r =>
                    {
                        Assert.AreEqual(r, 123);
                        this.EnqueueTestComplete();
                    }
                );
        }

        [TestMethod]
        [Asynchronous]
        public void OperatoionWithOperatoionWithPrimitiveInputParameter()
        {
            var restService = new FooServiceClient();
            IDisposable result = restService.OperatoionWithPrimitiveInputParameter(5)
                .Subscribe
                (
                    r =>
                    {
                        Assert.AreEqual(r, 123);
                        this.EnqueueTestComplete();
                    }
                );
        }

        [TestMethod]
        [Asynchronous]
        public void OperatoionWithArrayParameter()
        {
            var restService = new FooServiceClient();
            IDisposable result = restService.OperatoionWithArrayParameter(new List<int> {1,3,423,3,5})
                .Subscribe
                (
                    r =>
                    {
                        Assert.AreEqual(r, 123);
                        this.EnqueueTestComplete();
                    }
                );
        }

        [TestMethod]
        [Asynchronous]
        public void OperationWithComplexResponse()
        {
            var restService = new FooServiceClient();
            IDisposable result = restService.OperationWithComplexResponse()
                .Subscribe
                (
                    r =>
                    {
                        Assert.AreEqual(r.Name, "Apple");
                        Assert.AreEqual(r.Price, 300);
                        this.EnqueueTestComplete();
                    }
                );
        }
    }

    public class FooParameter
    {
        public string Name { get; set; }

        public int Price { get; set; }
    }

    public class FooServiceClient
    {
        [HttpPost]
        public IObservable<int> OperatoionWithComplexInputParameter(FooParameter param)
        {
            return Rest.GenerateOperation<FooParameter, int>(this.OperatoionWithComplexInputParameter)(param);
        }

        [HttpPost]
        public IObservable<int> OperatoionWithComplexInputParameterSendedViaRequestStream([RequestParameter] FooParameter param)
        {
            return Rest.GenerateOperation<FooParameter, int>(this.OperatoionWithComplexInputParameterSendedViaRequestStream)(param);
        }

        [HttpPost]
        public IObservable<int> OperatoionWithPrimitiveInputParameter(int param)
        {
            return Rest.GenerateOperation<int, int>(this.OperatoionWithPrimitiveInputParameter)(param);
        }

        [HttpPost]
        public IObservable<int> OperatoionWithArrayParameter(IEnumerable<int> param)
        {
            return Rest.GenerateOperation<IEnumerable<int>, int>(this.OperatoionWithArrayParameter)(param);
        }

        [HttpPost]
        public IObservable<FooParameter> OperationWithComplexResponse()
        {
            return Rest.GenerateOperation<FooParameter>(this.OperationWithComplexResponse)();
        }
    }
}